package com.financing.wap.service.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.financing.wap.common.Session;
import com.financing.wap.common.SessionUtil;
import com.financing.wap.dao.IFpBiddingReceiptDao;
import com.financing.wap.dto.FpBiddingReceiptDTO;
import com.financing.wap.dto.FpBiddingRevenueDTO;
import com.financing.wap.dto.UserDTO;
import com.financing.wap.service.IFpBiddingReceiptService;
import com.financing.wap.util.DTOdateSetUtil;
import com.financing.wap.util.SequenceManagerNew;



@Service("fpBiddingReceiptServiceImpl")
public class FpBiddingReceiptServiceImpl implements IFpBiddingReceiptService{

	@Autowired
	IFpBiddingReceiptDao fpBiddingReceiptDao;
	
	@Override
	public Integer addFpBiddingReceipt(FpBiddingReceiptDTO dto) {
		return fpBiddingReceiptDao.addFpBiddingReceipt(dto);
	}

	@Override
	public List<Map<String, Object>> queryFpBiddingReceipt(
			FpBiddingReceiptDTO dto) {
		return fpBiddingReceiptDao.queryFpBiddingReceipt(dto);
	}

	@Override
	@Transactional(rollbackFor=Exception.class)
	public Integer addFpBiddingReceiptBatch(JSONArray jsonArray,FpBiddingReceiptDTO fpBiddingReceiptDto) {
		FpBiddingReceiptDTO fbrd=new FpBiddingReceiptDTO();
		// 初始化属性
		DTOdateSetUtil.setCreateInfo(fbrd);
		DTOdateSetUtil.setUpdateInfo(fbrd);
		Session session = SessionUtil.getSession();//获取登录信息
		UserDTO loginUser = session.getUser();
		fbrd.setSrouce(fpBiddingReceiptDto.getSrouce());//来自 pc还是手机
		if("true".equals(fpBiddingReceiptDto.getFlagAnonymous())){
			fpBiddingReceiptDto.setFlagAnonymous("1");
		}else if("false".equals(fpBiddingReceiptDto.getFlagAnonymous())){
			fpBiddingReceiptDto.setFlagAnonymous("2");
		}
		fbrd.setFlagAnonymous(fpBiddingReceiptDto.getFlagAnonymous());//是否匿名
		fbrd.setFlagBail("2");//默认未交保证金
		fbrd.setBiddingStatus("1");//默认为提交竞价
		fbrd.setFaId(loginUser.getId());
		fbrd.setpId(fpBiddingReceiptDto.getpId());
//		fbrd.setBiddingCode(SequenceManagerNew.seq_bzj());//竞价code   保证金支付使用
		fbrd.setBiddingCode(fpBiddingReceiptDto.getBiddingCode());
		//计算保证金金额
		fbrd.setBailAmount(fpBiddingReceiptDto.getBailAmount());
		
		//新增竞价
		int biddingReceiptId=fpBiddingReceiptDao.addFpBiddingReceipt(fbrd);
		
		//新增  竞价佣金
		FpBiddingRevenueDTO revenueDto=new FpBiddingRevenueDTO();
		revenueDto.setReceiptId(biddingReceiptId);
		// 初始化属性
		DTOdateSetUtil.setCreateInfo(revenueDto);
		DTOdateSetUtil.setUpdateInfo(revenueDto);
		for (int i = 0; i < jsonArray.size(); i++) {
			JSONObject object = jsonArray.getJSONObject(i);
			Integer id = object.getInteger("id");//佣金id
			BigDecimal rebate = object.getBigDecimal("rebate");//竞价返佣
			revenueDto.setAnnualRevenueId(id);
			revenueDto.setpRebateAmount(rebate);
			fpBiddingReceiptDao.addFpBiddingRevenue(revenueDto);
		}
		return biddingReceiptId;
	}

	@Override
	public Map<String, Object> queryBiddingReceiptUserInfo(
			FpBiddingReceiptDTO dto) {
		return fpBiddingReceiptDao.queryBiddingReceiptUserInfo(dto);
	}

	@Override
	public List<Map<String, Object>> queryFpCurrentAnune(FpBiddingReceiptDTO dto) {
		return fpBiddingReceiptDao.queryFpCurrentAnune(dto);
	}

	@Override
	public int updateBiddingReceipt(FpBiddingReceiptDTO dto) {
		return fpBiddingReceiptDao.updateBiddingReceipt(dto);
	}

	@Override
	public FpBiddingReceiptDTO queryFpBiddingReceiptInfo(FpBiddingReceiptDTO dto) {
		return fpBiddingReceiptDao.queryFpBiddingReceiptInfo(dto);
	}

	@Override
	public Map<String, Object> queryFpCurrentAnuneById(Integer id) {
		return fpBiddingReceiptDao.queryFpCurrentAnuneById(id);
	}
	
}
